package com.ctgu.chatapp.view.DialogFragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import com.ctgu.chatapp.Service.RequestService
import com.ctgu.chatapp.databinding.DialogResponseBinding
import com.ctgu.chatapp.model.Database.DataBaseManager
import com.ctgu.chatapp.model.Retrofit.IMSServiceImpl
import com.ctgu.chatapp.model.pojo.FriendShip
import com.ctgu.chatapp.view.IMSApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class Response(val chatID: Long, val friendID: Long) : DialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = DialogResponseBinding.inflate(inflater, container, false)
        binding.addinfoSent.setOnClickListener {
            if (binding.addinfoGroup.text.toString().isNotEmpty()) {
                CoroutineScope(IO).launch {
                    val result = IMSServiceImpl.imsServiceImpl.service.getInformation(friendID)
                    async {
                        DataBaseManager.getInstance(IMSApplication.context).getInformationDao()
                            .addInformation(result)
                    }
                    async {
                        DataBaseManager.getInstance(IMSApplication.context).getFriendDao()
                            .addFriendOne(
                                FriendShip(
                                    null,
                                    chatID,
                                    friendID,
                                    binding.addinfoGroup.text.toString()
                                )
                            )
                    }
                    DataBaseManager.getInstance(IMSApplication.context).getRequestDao()
                        .updateFriendRequest(1, chatID, friendID)
                    RequestService.sendFriendResponse(
                        chatID,
                        friendID, 1,
                        binding.addinfoGroup.text.toString()
                    )
                }
            }
            this.onDismiss(requireDialog())
        }
        binding.addinfoCancel.setOnClickListener {
            this.onDismiss(requireDialog())
            CoroutineScope(IO).launch {
                DataBaseManager.getInstance(IMSApplication.context).getRequestDao()
                    .updateFriendRequest(2, chatID, friendID)
                RequestService.sendFriendResponse(
                    chatID,
                    friendID, 2,
                    binding.addinfoGroup.text.toString()
                )
            }
        }
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        val params = dialog!!.window!!.attributes
        params.width = 350
        dialog!!.window!!.attributes = params as WindowManager.LayoutParams
    }
}